﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

namespace FourPartHarmony3 {
    class VirtualShape : IVirtualChild {
        public enum Type {
            Rectangle,
            TextBlock,
        }
        Type mType;
        UIElement mVisual;

        /// <summary>
        /// Rectangle用コンストラクタ
        /// </summary>
        public VirtualShape(Type type, Rect bounds) {
            mType = type;
            Bounds = bounds;
            ForegroundBrush = Brushes.Black;
            StrokeThickness = 0.0;
            StrokeDashArray = new DoubleCollection();
        }
        
        /// <summary>
        /// TextBlock用コンストラクタ。あとでSetTextを呼び出す必要がある。
        /// </summary>
        public VirtualShape(Type type, Vector xy) {
            mType = type;
            Bounds = new Rect(xy.X, xy.Y, Int16.MaxValue, Int16.MaxValue);
            ForegroundBrush = Brushes.Black;
            StrokeThickness = 0.0;
            StrokeDashArray = new DoubleCollection();
        }

        public void SetText(string text, double fontSize) {
            Text = text;
            FontSize = fontSize;
            MeasureBoundary();
        }

        public double StrokeThickness { get; set; }
        public DoubleCollection StrokeDashArray { get; set; }
        public Brush Stroke { get; set; }

        public string Text { set; get; }

        /// <summary>
        /// pixel in 96dpi
        /// </summary>
        public double FontSize { set; get; }
        public Brush ForegroundBrush { get; set; }

        public UIElement Visual {
            get { return mVisual; }
        }

        public void DisposeVisual() {
            mVisual = null;
        }

        public Rect Bounds { get; set; }

        public bool Focusable { get; set; }

        public UIElement CreateVisual(HorizontalVirtualCanvas parent) {
            if (mVisual != null) {
                return mVisual;
            }

            switch (mType) {
            case Type.Rectangle:
                mVisual = CreateRectangle();
                break;
            case Type.TextBlock:
                var tb = CreateTextBlock();
                if (0 < StrokeThickness) {
                    var b = new Border();
                    b.BorderThickness = new Thickness(StrokeThickness);
                    b.BorderBrush = Stroke;
                    b.Child = tb;
                    mVisual = b;
                } else {
                    mVisual = tb;
                }
                break;
            }
            return mVisual;
        }

        private UIElement CreateRectangle() {
            var r = new Rectangle();
            r.Width = Bounds.Width;
            r.Height = Bounds.Height;
            r.Fill = ForegroundBrush;
            if (StrokeThickness != 0) {
                r.StrokeThickness = StrokeThickness;
                r.StrokeDashArray = StrokeDashArray;
                r.Stroke = Stroke;
            }
            return r;
        }

        private UIElement CreateTextBlock() {
            var tb = new TextBlock();
            tb.Text = Text;
            tb.FontSize = FontSize;
            tb.Foreground = ForegroundBrush;

            tb.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
            var fontWH = tb.DesiredSize;
            Bounds = new Rect(Bounds.X, Bounds.Y, fontWH.Width, fontWH.Height);
            return tb;
        }

        public void MeasureBoundary() {
            switch (mType) {
            case Type.TextBlock:
                var tb = CreateTextBlock();
                tb = null;
                break;
            case Type.Rectangle:
                break;
            }
        }
    }
}
